package vn.orderonline.com.core.business.member;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import vn.orderonline.core.commons.utils.DateUtils;
import vn.orderonline.core.commons.utils.StringUtils;
import vn.orderonline.core.dao.entity.Member;
import vn.orderonline.core.dao.member.MemberDao;
import vn.orderonline.core.redis.RedisMember;
import vn.orderonline.core.redis.UserRedisService;

@Service
@Transactional
public class MemberImpl implements IMember {

	@Autowired
	private MemberDao memberDao;
	@Autowired
	private UserRedisService userRedisService;

	@Override
	public List<RedisMember> getAllMember() {
		List<RedisMember> list = new ArrayList<>();
		List<Member> listMember = memberDao.findAll();
		if (!listMember.isEmpty()) {
			RedisMember redisMember = null;
			for (Member me : listMember) {
				redisMember = new RedisMember();
				redisMember.setMemberId(me.getMemberId());
				redisMember.setMemberName(me.getMemberName());
				redisMember.setMemberEmail(me.getMemberEmail());
				redisMember.setUsername(me.getUsername());
				redisMember.setCreateTime(DateUtils.timestampToString(
						me.getCreateTime(), "dd/MM/yyyy HH:mm:ss"));
				redisMember.setPhoneNumber(me.getPhoneNumber());
				redisMember.setWardName(me.getWardName());
				list.add(redisMember);
			}
		}

		return list;
	}

	@Override
	public RedisMember getMemberByUsername(String username) {
		RedisMember redisMember = null;
		if (!StringUtils.isNullOrEmpty(username)) {
			redisMember = userRedisService.getUser(username);
		}
		return redisMember;
	}

	@Override
	public Boolean checkExistedEmail(String email) {
		if (!StringUtils.isNullOrEmpty(email)) {
			return memberDao.checkExistedEmail(email);
		}
		return false;
	}

	@Override
	public Boolean checkExistedUsername(String username) {
		if (!StringUtils.isNullOrEmpty(username)) {
			return memberDao.checkExistedUsername(username);
		}
		return false;
	}

	@Override
	public void addMember(RedisMember redisMember) {
		if (redisMember != null) {
			Member member = new Member();
			member.setMemberName(redisMember.getMemberName());
			member.setMemberEmail(redisMember.getMemberEmail());
			member.setUsername(redisMember.getUsername());
			member.setMemberPassword(redisMember.getMemberPassword());
			if (!StringUtils.isNullOrEmpty(redisMember.getBirthday())) {
				member.setBirthday(new Date((DateUtils.stringToDate(
						redisMember.getBirthday(), "yyyy-MM-dd").getTime())));
			}
			if (!StringUtils.isNullOrEmpty(redisMember.getGender())) {
				member.setGender(redisMember.getGender() == "1" ? true : false);
			}
			if (!StringUtils.isNullOrEmpty(redisMember.getWardName())) {
				member.setWardName(redisMember.getWardName());
			}
			member.setResideId(redisMember.getResideId());
			if (!StringUtils.isNullOrEmpty(redisMember.getPhoneNumber())) {
				member.setPhoneNumber(redisMember.getPhoneNumber());
			}
			member.setCreateTime(new Timestamp(DateUtils.now()));
			member.setCreateId(redisMember.getCreateId());
			member.setDescription(redisMember.getDescription());
			memberDao.save(member);
		}

	}

	@Override
	public void update(RedisMember member) {
		if (member != null) {
			List<Member> listMember = memberDao.findByProp("username",
					member.getUsername());
			if (!listMember.isEmpty()) {
				Member me = listMember.get(0);
				if (!StringUtils.isNullOrEmpty(member.getMemberEmail())) {
					me.setMemberEmail(member.getMemberEmail());
				}
				if (!StringUtils.isNullOrEmpty(member.getMemberName())) {
					me.setMemberName(member.getMemberName());
				}
				if (!StringUtils.isNullOrEmpty(member.getMemberPassword())) {
					me.setMemberPassword(member.getMemberPassword());
				}
				if (!StringUtils.isNullOrEmpty(member.getBirthday())) {
					me.setBirthday(new Date(DateUtils.stringToDate(
							member.getBirthday(), "yyyy-MM-dd").getTime()));
				}
				if (!StringUtils.isNullOrEmpty(member.getGender())) {
					me.setGender(member.getGender() == "1" ? true : false);
				}
				if (!StringUtils.isNullOrEmpty(member.getWardName())) {
					me.setWardName(member.getWardName());
				}
				if (member.getResideId() != null) {
					me.setResideId(member.getResideId());
				}
				if (!StringUtils.isNullOrEmpty(member.getPhoneNumber())) {
					me.setPhoneNumber(member.getPhoneNumber());
				}
				me.setUpdateTime(new Timestamp(DateUtils.now()));
				me.setUpdateId(member.getUpdateId());
				if (!StringUtils.isNullOrEmpty(member.getDescription())) {
					me.setDescription(member.getDescription());
				}
				memberDao.update(me);

				userRedisService.removeUser(me.getUsername(),
						me.getMemberEmail());
			}
		}

	}

	@Override
	public void delete(String username) {
		if (!StringUtils.isNullOrEmpty(username)) {
			RedisMember member = userRedisService.getUser(username);
			if (member != null) {
				userRedisService.removeUser(username, member.getMemberEmail());
				memberDao.delete(memberDao.findByProp("username", username)
						.get(0));
			}
		}

	}

	@Override
	public List<RedisMember> search(RedisMember member) {
		if (member != null) {
			List<RedisMember> list = memberDao.search(member.getUsername(),
					member.getMemberEmail(), member.getMemberName(),
					member.getPhoneNumber(), member.getWardName());
			return list;
		}
		return null;
	}

}
